﻿using BXH.Movie.Bll;
using BXH.Movie.Model;
using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BXH.Movie.FrmAdmin
{
    public partial class FrmMovieInfoAdd : CCWin.CCSkinMain
    {
        private MovieInfoService movieService = new MovieInfoService();
        public event EventHandler AddSuccess;
        public FrmMovieInfoAdd()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void InitializeParams()
        {
            this.txtRemark.Text = "";
            this.txtImage.Text = "";
            this.txtPrice.Text = "";
            this.txtDuration.Text = "";
            this.txtCountry.Text = "";
            this.txtAct.Text = "";
            this.txtDirect.Text = "";
            this.txtName.Text = "";
            this.txtDuration.Text = "";

        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            InitializeParams();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            MovieType type = cboType.SelectedItem as MovieType;

            MovieInfo movieInfo = new MovieInfo
            {
                Actor = txtAct.Text.Trim(),
                Direct = this.txtDirect.Text.Trim(),
                Name = this.txtName.Text.Trim(),
                Remark = this.txtRemark.Text.Trim(),
                Image = this.txtImage.Text.Trim(),
                Type = type.Id,
                Brief = txtBrief.Text,
                Country = txtCountry.Text
            };

            int time = 0;
            if (!int.TryParse(this.txtDuration.Text.Trim(), out time))
            {
                MessageBoxEx.Show("片长格式不正确");
                return;
            };
            movieInfo.Time = time.ToString();
            decimal ticketPrice;
            if (!decimal.TryParse(this.txtPrice.Text.Trim(), out ticketPrice))
            {
                MessageBoxEx.Show("价格格式不正确");
                return;
            }
            movieInfo.TicketPrice = ticketPrice;
            if (movieService.Add(movieInfo))
            {
                AddSuccess?.Invoke(this, new EventArgs());
                this.Close();
            }
        }

        private void btnUpLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                var imagePath = openFileDialog.FileName;
                var saveFileName = Guid.NewGuid().ToString() + Path.GetExtension(openFileDialog.SafeFileName);
                Image image = Image.FromFile(imagePath);
                var savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", saveFileName);
                image.Save(savePath);
                this.txtImage.Text = saveFileName;
            };
        }

        private void FrmMovieInfoAdd_Load(object sender, EventArgs e)
        {
            var types = movieService.GetAllMovieType();
            cboType.DataSource = types;
            cboType.DisplayMember = "TypeName";
        }
    }
}
